import { post, BaseRes } from "../libs/request";
import { Gender, Relevance } from "../models/enums";
import { IDynamic, IUser } from "../models/types";
// API路径前缀
const PREFIX = "/api/user";

interface User extends BaseRes {
  user: IUser;
}

export interface UList {
  _id: string;
  nickname: string;
  signature: string;
  avatar: string;
}

interface UserList extends BaseRes {
  data: { list: UList[]; total: number };
}
interface Dynamiclist extends BaseRes {
  data: { list: IDynamic[]; total: number };
}

export async function register(username: string, password: string) {
  const { data } = await post<BaseRes>(PREFIX + "/register", {
    username,
    password,
  });
  return data;
}
export async function login(username: string, password: string) {
  const { data } = await post<User>(PREFIX + "/login", {
    username,
    password,
  });
  return data;
}

export async function updatePwd(oldpwd: string, newpwd: string) {
  const { data } = await post<BaseRes>(PREFIX + "/update/pwd", {
    oldpwd,
    newpwd,
  });
  return data;
}

export async function updateinfo(
  nickname: string,
  signature: string,
  qqnumber: string,
  avatar: string,
  gender: Gender,
  major: string[]
) {
  const { data } = await post<User>(PREFIX + "/update/info", {
    nickname,
    signature,
    qqnumber,
    avatar,
    gender,
    major,
  });
  return data;
}

export async function getuser(token?: string) {
  const { data } = await post<User>(PREFIX + "/info", null, token);
  return data;
}

export async function otheruser(userId: string, token?: string) {
  const { data } = await post<User>(PREFIX + `/other/info`, { userId }, token);
  return data;
}

export async function logout() {
  const { data } = await post<User>(PREFIX + "/logout");
  return data;
}

export async function dealfocus(userId: string, flag: boolean) {
  const { data } = await post<BaseRes>(PREFIX + "/add/focus", { userId, flag });
  return data;
}

export async function focuslist(
  destId: string,
  skip: number,
  limit: number,
  flag: boolean
) {
  const { data } = await post<UserList>(PREFIX + "/focus", {
    destId,
    skip,
    limit,
    flag,
  });
  return data;
}
export async function dylist(
  userId: string,
  skip: number,
  limit: number,
  type?: Relevance
) {
  const { data } = await post<Dynamiclist>(PREFIX + "/dynamic", {
    userId,
    skip,
    limit,
    type,
  });
  return data;
}
